#pragma once

#ifndef _CR_HTTP_PAYLOAD_H_
#define _CR_HTTP_PAYLOAD_H_

NS_CR_BEGIN

namespace http
{
	struct Pair 
	{
		template <typename KeyType
			, typename ValueType
			, typename _STD enable_if<!_STD is_integral<ValueType>::value, bool>::type = true>
		Pair(KeyType&& p_key, ValueType&& p_value)
			: key{ _STD_FWD(p_key) }
			, value{ _STD_FWD(p_value) }
		{}
		
		template <typename KeyType>
		Pair(KeyType&& p_key, const int32_t& p_value)
			: key{ _STD_FWD(p_key) }
			, value{ _STD to_string(p_value) }
		{}

		string key;
		string value;
	};

	class Payload
	{
	public:
		template <class It>
		Payload(const It begin, const It end) 
		{
			for (It pair = begin; pair != end; ++pair) 
			{
				AddPair(*pair);
			}
		}

		Payload(const _STD initializer_list<Pair>& pairs)
			: Payload(begin(pairs), end(pairs))
		{}

		void AddPair(const Pair& pair)
		{
			if (!content.empty()) 
			{
				content += "&";
			}
			auto escaped = utils::urlEncode(pair.value);
			content += pair.key + "=" + escaped;
		}

		string content;
	};
}

NS_CR_END

#endif // !_CR_HTTP_PAYLOAD_H_

